﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.AdvTree;

namespace TextEditor.TextDocument.Data.Knowledge
{
    public partial class KnowledgeControl : Form
    {
        public delegate void InsertKnowledge(string value, string data, string type,string name);
        public delegate void IsNotSelected();
        public InsertKnowledge OnInsertKnow;
        public IsNotSelected OnNotSelected;
        public KnowledgeControl()
        {
            InitializeComponent();
            this.IniKnowledgeTree.DoubleClick += new EventHandler(IniKnowledgeTree_DoubleClick);
        }

        void IniKnowledgeTree_DoubleClick(object sender, EventArgs e)
        {
            OnEnterOk();
        }

        public void updatePoint(Point point, int width, int height)
        {
            this.Location = GetPopupPos(point.X, point.Y, width + 2, height + 2);
        }

        public Point GetPopupPos(int x, int y,int _width, int height)
        {
            int width = Screen.PrimaryScreen.Bounds.Width;
            int num2 = Screen.PrimaryScreen.Bounds.Height;
            int num3 = base.Width;
            int num4 = base.Height;
            Point point = new Point(x + _width, y + height);
            if (point.X < 0)
            {
                point.X = 0;
            }
            if (point.Y < 0)
            {
                point.Y = 0;
            }
            if (((y + height) + num4) > num2)
            {
                point.Y = y - num4;
            }
            if ((x + num3) > width)
            {
                point.X = width - num3;
            }
            return point;
        }

        private void OnEnterOk()
        {
            Node node = this.IniKnowledgeTree.SelectedNode;
            if (node != null)
            {
                if (node.Level == 0)
                {
                    if (node.Expanded)
                        node.Collapse();
                    else
                        node.Expand();
                }
                else
                {
                    if (this.OnInsertKnow != null)
                    {
                        this.Hide();
                        OnInsertKnow(node.Text,node.Parent.Tag.ToString(),node.Tag.ToString(),node.Parent.Text);
                        this.IniKnowledgeTree.Nodes.Clear();
                    }
                }
            }
        }

        public void HidebackShow()
        {
            this.Hide();
            if (OnNotSelected != null)
            {
                OnNotSelected();
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            HidebackShow();
        }

        private void KnowledgeControl_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyData == Keys.Escape)
                HidebackShow();
            if (e.KeyData == Keys.Enter)
                OnEnterOk();
        }

        private void IniKnowledgeTree_KeyDown(object sender, KeyEventArgs e)
        {
            KnowledgeControl_KeyDown(sender, e);
        }

        private void 刷新知识库ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Singleton<KnowledgeBase>.Instance.RefreshData();
        }
    }
}
